package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.feign.BehaviorFeign;
import com.heima.article.mapper.ApCollectionBehaviorMapper;
import com.heima.article.service.ApCollectionBehaviorService;
import com.heima.model.article.dtos.CollectionBehaviorDto;
import com.heima.model.article.pojos.ApCollection;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadlocal.AppThreadLocalUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Log4j2
public class ApCollectionBehaviorServiceImpl extends ServiceImpl<ApCollectionBehaviorMapper, ApCollection> implements ApCollectionBehaviorService {

    @Autowired
    private BehaviorFeign behaviorFeign;

    /**
     * 保存收藏行为
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult collection(CollectionBehaviorDto dto) {
        //校验参数
        if (dto == null || dto.getArticleId() == null || dto.getEquipmentId() == null
                || (dto.getOperation() > 1 && dto.getOperation() < 0)
                || (dto.getType() > 1 && dto.getType() < 0)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUser apUser = AppThreadLocalUtils.getUser();
        ApBehaviorEntry apBehaviorEntry = null;
        //校验实体
        if (apUser == null) {
            apBehaviorEntry = behaviorFeign.findByUserIdOrEquipmentId(null, dto.getEquipmentId());
        } else {
            apBehaviorEntry = behaviorFeign.findByUserIdOrEquipmentId(apUser.getId(), dto.getEquipmentId());
        }
        if (apBehaviorEntry == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //保存收藏行为
        ApCollection apCollection = getOne(Wrappers.<ApCollection>lambdaQuery()
                .eq(ApCollection::getEntryId, apBehaviorEntry.getId())
                .eq(ApCollection::getArticleId, dto.getArticleId()));
        if (apCollection == null) {
            //保存收藏行为
            apCollection = new ApCollection();
            apCollection.setArticleId(dto.getArticleId());
            apCollection.setCollectionTime(new Date());
            apCollection.setEntryId(apBehaviorEntry.getId());
            apCollection.setPublishedTime(dto.getPublishedTime());
            apCollection.setType(dto.getType());
            save(apCollection);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        } else {
            //修改收藏行为
            apCollection.setType(dto.getType());
            updateById(apCollection);
            /*apCollection.setOperation(dto.getOperation());
            updateById(apCollection);*/
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
    }
}
